{$mode delphi}

{
  $Id: cmdline.pas 4 2006-10-05 09:18:53Z kcynarski $
  $HeadURL: http://isbuilder.googlecode.com/svn/trunk/cmdline.pas $
  Inno Setup Builder
  Command Line Parser
}

unit cmdline;

interface

uses sysutils, classes;

type TCommandLineParser = class
     private
       VParams : TStringList;
       VCommands : TStringList;
       {--}
       function GetCommandCount:integer;
       function GetCommandState(key:string):boolean;
       function GetCommand(index:integer):string;
       function GetParamState(key:string):boolean;
       function GetParamValue(key:string):string;
     public
       constructor Create;
       destructor Destroy; override;
       {--}
       property CommandCount:integer read GetCommandCount;
       property CommandExists[key:string]:boolean read GetCommandState;
       property Command[index:integer]:string read GetCommand;
       property ParamExists[key:string]:boolean read GetParamState;
       property Param[key:string]:string read GetParamValue;
     end;
                          
implementation

function TCommandLineParser.GetCommandCount : integer;
begin
     result:=VCommands.Count;
end;
                          
function TCommandLineParser.GetCommandState(key:string):boolean;
var i:integer;
begin
     result:=false;
     for i:=0 to VCommands.Count-1 do
      if UpCase(VCommands[i])=UpCase(key) then result:=true;
end;

function TCommandLineParser.GetCommand(index:integer):string;
begin
     if index=-1 then index:=VCommands.Count;
     result:=VCommands[index-1];
end;

function TCommandLineParser.GetParamState(key:string):boolean;
begin
     result:=(VParams.Values[key]<>'');
end;

function TCommandLineParser.GetParamValue(key:string):string;
begin
     result:=VParams.Values[key];
end;
                          
function StripQuotes(s:string):string;
begin
  if s[1]='"' then delete(s,1,1);
  if s[length(s)]='"' then delete(s,length(s),1);
  result := s;
end;
                          
constructor TCommandLineParser.Create;
var i,q:integer;
    s:string;
begin
     VParams := TStringList.Create;
     VCommands := TStringList.Create;
     for i:=1 to paramcount do
      begin
        if paramstr(i)[1]<>'/' then
           VCommands.Add(paramstr(i));
        if paramstr(i)[1]='/' then
           begin
           s:=paramstr(i); delete(s,1,1);
           q:=pos('=',s);
           if q>0 then
              begin
               VParams.Add(copy(s,1,q-1)+'='+copy(s,q+1,length(s)-q));
              end
              else
              begin
               VParams.Add(s+'=*NONE*');
              end;
           end
      end;
end;

destructor TCommandLineParser.Destroy;
begin
     VParams.Free;
     VCommands.Free;
end;

begin
end.